﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using Mighty.Lib.SensedTypes;
using Mighty.Lib.Document;

namespace Mighty.Lib
{



    /// <summary>
    /// Represents a tightly coupled document + form control that contains all of the 
    /// goods to edit/save/create a document in ManagEdit as well as the things
    /// neccesary to get that tasty code-discovery functionality
    /// </summary>
    public class DefaultDocument : IDocument
    {

        private List<string> _rawDocument = null;

        private string _fullPath = "";
        public string FullPath
        {
            get
            {
                return _fullPath;
            }
            set
            {
                _fullPath = value;
            }
        }

        private bool _hasBeenSavedBefore = false;
        public bool HasBeenSavedBefore
        {
            get
            {
                return _hasBeenSavedBefore;
            }
            set
            {
                _hasBeenSavedBefore = value;
            }
        }
        
        private string _parseType = "";
        public string ParseType
        {
            get
            {
                return _parseType;
            }
            set
            {
                _parseType = value;
            }
        }
        private string _docName = "";
        public string Name
        {
            get
            {
                return _docName;
            }
            set
            {
                _docName = value;
            }
        }
        

        /// <summary>
        ///  default ctor... spins up a new document
        /// </summary>
        public DefaultDocument()
        {
            // empty list
            List<string> list = new List<string>();
            string docName = "New Document";
            SetupDocContents(list,docName, "none");
            
            
        }

        /// <summary>
        /// Spins up a Document instance loaded with the contents of a file
        /// </summary>
        /// <param name="filePath"></param>
        public DefaultDocument(IOpenProvider loadProvider, Dictionary<string, object> loadParameters)
        {
            string docName = (string) loadParameters["FileName"];
            List<string> rawDoc = loadProvider.Open(loadParameters);
            
            SetupDocContents(rawDoc, docName, (new FileInfo((string)loadParameters["FileName"])).Extension);

        }

        public string GetParseTypeFromFileName(string fileName)
        {
            string[] split = fileName.Split(new char[]{'.'});
            return split[split.Length - 1];
        }
        

        public void SetupDocContents(List<string> rawDoc, string docName, string parseType)
        {
            _docName = docName;
            _parseType = parseType;
            _rawDocument = rawDoc;
        }

        private RichTextBox _richTextBox = null;
        public RichTextBox RichTextBox
        {
            get
            {
                return _richTextBox;
            }
            set
            {
                _richTextBox = value;
            }
        }




        #region IDocument Members

        public void ResizeHandler()
        {
            throw new NotImplementedException();
        }

        public List<string> Contents
        {
            get { return _rawDocument; }
            set { _rawDocument = null; _rawDocument = value; }
        }

        public List<Namespace> LoadedNamespaces
        {
            get { throw new NotImplementedException(); }
        }

        public IDocumentModel DocumentModel
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
